﻿Imports System.Runtime.CompilerServices
Imports System.Reflection

''' <summary>
''' プロパティ拡張メソッド
''' </summary>
''' <remarks></remarks>
Friend Module PropertyInfoExtension

    ''' <summary>
    ''' Nullable型かどうかを判定します
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function IsNullable(source As PropertyInfo) As Boolean
        'カスタム属性がある場合、許容
        If source.GetCustomAttributes(GetType(NullableAttribute), False).Any Then Return True

        If source.PropertyType.IsGenericType = False Then Return False

        If source.PropertyType.GetGenericTypeDefinition Is GetType(Nullable(Of )) Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' コマンドキー属性を取得します
    ''' </summary>
    ''' <param name="prop"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function ToCommandKey(prop As PropertyInfo) As CommandKeyAttribute
        Static caches As New Dictionary(Of PropertyInfo, CommandKeyAttribute)

        If caches.ContainsKey(prop) = False Then
            Dim atr = (From el In prop.GetCustomAttributes(GetType(CommandKeyAttribute), False) Select DirectCast(el, CommandKeyAttribute)).FirstOrDefault
            caches.Add(prop, atr)
        End If

        Return caches(prop)
    End Function
End Module